package uoc.biblioteca.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

import uoc.biblioteca.mappings.dao.Code1Home;
import uoc.biblioteca.mappings.object.MilleniumVo;

public class ReaderTxt {

	private String inputFile;

	public void setInputFile(String inputFile) {
		this.inputFile = inputFile;
	}

	public List<MilleniumVo> read() throws IOException  {
		
	
	    StringBuilder text = new StringBuilder();
	    List<MilleniumVo> datos2 = new ArrayList<MilleniumVo>();
	    
	   try{
	    
	   File file =  new File(inputFile);
	    //MilleniumStructure structure = new MilleniumStructure();
	    Scanner s = new Scanner(file,"UTF8");
	   
	  
	    //try {
	    	
	      int j=0; //Numero de registros leidos
	    	
	      while (s.hasNextLine()){
	    	  MilleniumVo structure = new MilleniumVo();
	    	  text = new StringBuilder();
	    	  text.append(s.nextLine());
	      
  			  
  			  StringTokenizer tokens=new StringTokenizer(text.toString(),"|");
  	         
  	          int x=0; //Numero de campos leidos por cada registro
  	          
  	          while(tokens.hasMoreTokens()){
  	              String str=tokens.nextToken();
  	              str=str.replaceAll("\"", "");
  	              str=str.trim();
  	             
  	              //Ignorar la primera linea
  	              if(j!=0){
  	            	  if(x==0){
  	            		  structure.setFondo(str);
  	            	  }
  	            	  else if(x==1){
  	            		  structure.setCode1(str);
  	            	  }
  	            	  else if(x==2){
  	            		  structure.setCodigo2(str);
  	            	  }
  	            	  else if(x==3){
  	            		  structure.setNregistro(str);
  	            	  }
  	            	  else if(x==4){
  	            		  structure.setComEuros(str);
  	            	  }
//  	            	  else if(x==5){
//  	            		  structure.setStandard(str);
//  	            	  }
  	            	  else if(x==5){
  	            		  structure.setAutor(str);
  	            	  }
  	            	  else if(x==6){
  	            		  structure.setTitulo(str);
  	            	  }
//  	            	  //CAMBIARA PUBLICACIO
//  	            	  else if(x==8){
//  	            		  structure.setPublicacio(str);
//  	            	  }
  	            	  else if(x==7){
  	            		  structure.setEjemplares(str);
  	            	  }
  	            	  else if(x==8){
  	            		  structure.setPrecioEst(str);
  	            	  }
  	            	  else if(x==9){
  	            		  structure.setNotaProv(str);
  	            	  }
  	            	  else if(x==10){
  	            		  structure.setProject(str);
  	            	  }
  	            	  else if(x==11){
  	            		  structure.setIsbn2(str);
  	            	  }
  	            	  else if(x==12){
  	            		  structure.setProveedor(str);
  	            	  }
  	            	  else if(x==13){
  	            		  structure.setNotaPed(str);
  	            	  }
  	            	else if(x==14){
	            		  structure.setBlanketPo(str);
	            	  }
  	            	else if(x==15){
	            		  structure.setEstado(str);
	            	  }
  	            	else if(x==16){
	            		  structure.setFechaRec(str);
	            	  }
  	            	else if(x==17){
	            		  structure.setActualiz(str);
	            	  }
  	            	else if(x==18){
	            		  structure.setPoInfo(str);
	            	  }
  	            	else if(x==19){
	            		  structure.setNregistroBiblio(str);
	            	  }
  	            	else if(x==20){
	            		  structure.setIsbn(str);
	            	  }
  	            	else if(x==21){
	            		  structure.setIssn(str);
	            	  }
  	            	else if(x==22){
	            		  structure.setMatType(str);
	            	  }
  	            	else if(x==23){
	            		  structure.setLloc(str);
	            	  }
  	            	else if(x==24){
	            		  structure.setPublicacio(str);
	            	  }
  	            	else if(x==25){
	            		  structure.setAny(str);
	            	  }
  	            	else if(x==26){
	            		  structure.setIsReclamat(str);
	            	  }
  	            	else if(x==27){
	            		  structure.setIsCatalogat(str);
	            	  }
  	            	else if(x==28){
	            		  structure.setUrl(str);
	            		  datos2.add(structure);
	            		  x=0;
	            	  }
  	            	  x++;
  	              }
  	          }
  	        j++;
	      } 
	      s.close();  
  		  //}
	    }
	    
	    catch (Exception e) {
	    	e.printStackTrace();
		}
	    
	    finally{
	      //s.close();
	    }
	    
	    return datos2;
	    
	}

	
	public String generateNRef(MilleniumVo millenium){
		
		Code1Home code1Home = new Code1Home();
		
		String nRef=null;
		
		if(("b1").equals(millenium.getFondo())){
			if(("4").equals(millenium.getCode1()) || ("8").equals(millenium.getCode1()) || ("3").equals(millenium.getCode1()) || ("a").equals(millenium.getCode1())  ){
				nRef = code1Home.getDescCode1(millenium.getCode1())+"-"+millenium.getFondo();
			}
			else{
				nRef = millenium.getFondo() +"-"+ code1Home.getDescCode1(millenium.getCode1());
			}
		}
		if(("n1").equals(millenium.getFondo())){
			if(millenium.getComEuros()==null){
				if(("-").equals(millenium.getCode1())){
					nRef = millenium.getProject()+"-"+millenium.getFondo();
				}
				else{
					nRef = code1Home.getDescCode1(millenium.getCode1())+"-"+millenium.getFondo();
				}
			}
			else{
				if(("-").equals(millenium.getCode1())){
					nRef = millenium.getComEuros()+"-"+millenium.getProject();
				}
				else{
					nRef = millenium.getComEuros()+"-"+code1Home.getDescCode1(millenium.getCode1());
				}
			}
		}
		else{
			if(("-").equals(millenium.getCode1())){
				nRef = millenium.getFondo();
			}
			else{
				nRef = millenium.getFondo() +"-"+ code1Home.getDescCode1(millenium.getCode1());
			}
		}
		
		return nRef;
	}
	
	
}
